﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XB.Core.MemoryStore.TaskTimer
{
    public class CountdownTimer : ITaskTimer
    {
        private int _interval;
        private static DateTime startTime = new DateTime(1900, 1, 1);
        private DateTime nextRunTime;
        private bool isForceRun = false;

        public CountdownTimer(int interval)
        {
            nextRunTime = startTime;
            _interval = interval;
        }

        public void Reset(DateTime now)
        {
            isForceRun = false;
            nextRunTime = now.AddSeconds(_interval - (now - startTime).TotalSeconds % _interval);
        }

        public bool TimeOut(DateTime now)
        {
            return isForceRun || now > nextRunTime;
        }

        public void ForceRun()
        {
            isForceRun = true;
        }
    }
}
